package com.company.cloud.coupon.modules.mgr.controller;

import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.LocalDateTimeUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.coupon.constant.CouponConstant.CouponActStateEnum;
import com.company.cloud.coupon.constant.CouponErrorEnum;
import com.company.cloud.coupon.modules.base.entity.CouponCarveupConfigInfo;
import com.company.cloud.coupon.modules.base.service.CouponCarveupConfigInfoService;
import com.company.cloud.coupon.modules.cache.models.CouponCarveUpConfigModel;
import com.company.cloud.coupon.modules.cache.utils.CouponChangeUtils;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponCarveUpConfigQuery4PageParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponCarveUpConfigSaveParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponCarveUpConfigUpdateParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 瓜分券活动配置管理
 */
@Authc
@RestController
@RequestMapping("/mgr/op/carveUpConfig")
@Api(tags="OP_Mgr_瓜分券活动配置管理")
public class CouponCarveUpConfigMgrController {

	@Autowired
	private CouponCarveupConfigInfoService couponCarveupConfigInfoService;
	
	@PostMapping("/query")
	@ApiOperation(value="查询", response=CouponCarveUpConfigModel.class)
	public BaseResult query(@RequestBody CouponCarveUpConfigQuery4PageParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<CouponCarveupConfigInfo> page = couponCarveupConfigInfoService.page(new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<CouponCarveupConfigInfo>()
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				.eq("company_no", userInfo.getCompanyNo())
				.like(StringUtils.hasText(params.getName()), "name", params.getName())
				.eq(StringUtils.hasText(params.getState()), "state", params.getState())
				.orderByDesc("cdt")
				);
		IPage<CouponCarveUpConfigModel> data=page.convert(new Function<CouponCarveupConfigInfo, CouponCarveUpConfigModel>() {
			@Override
			public CouponCarveUpConfigModel apply(CouponCarveupConfigInfo entity) {
				return CouponChangeUtils.getCouponCarveUpConfig(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	@PostMapping("/add")
	@ApiOperation(value="新增", response=BaseResult.class)
	public BaseResult add(@RequestBody CouponCarveUpConfigSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		CouponCarveupConfigInfo entity=new CouponCarveupConfigInfo();
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setName(params.getName());
		entity.setIsUseLimit(params.getIsUseLimit());
		entity.setUseFullFund(params.getUseFullFund());
		entity.setTotalNum(params.getTotalNum());
		entity.setGetNum(0);
		entity.setStartDate(LocalDateTimeUtils.parse_yyyyMMdd(params.getStartDate()));
		entity.setEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getEndDate()));
		entity.setIndateDay(params.getIndateDay());
		entity.setIndateHour(params.getIndateHour());
		entity.setIndateMinute(params.getIndateMinute());
		entity.setCarveupUserNum(params.getCarveupUserNum());
		entity.setCarveupFund(params.getCarveupFund());
		entity.setCarveupWay(params.getCarveupWay());
		entity.setUsableObjType(params.getUsableObjType());
		entity.setUsableObjNo(StrSplitUtils.list2string(params.getUsableObjNo()));
		entity.setUseDateType(params.getUseDateType());
		entity.setUseStartDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseStartDate()));
		entity.setUseEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseEndDate()));
		entity.setUseEndDay(params.getUseEndDay());
		entity.setSimulateFriendSetting(params.getSimulateFriendSetting());
		entity.setMemberGetNumType(params.getMemberGetNumType());
		entity.setMemberGetNum(params.getMemberGetNum());
		entity.setIsRemind(params.getIsRemind());
		entity.setRemindDay(params.getRemindDay());
		entity.setOtherSetting(StrSplitUtils.list2string(params.getOtherSetting()));
		if(StringUtils.isEmpty(params.getState())) {
			entity.setState(CouponActStateEnum.CFS_ING.getCode());
		}else {
			entity.setState(params.getState());
		}
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		couponCarveupConfigInfoService.save(entity);
		return BaseResult.ok();
	}
	
	@PostMapping("/edit")
	@ApiOperation(value="修改", response=BaseResult.class)
	public BaseResult edit(@RequestBody CouponCarveUpConfigUpdateParam params) {
		ValidatorUtils.validate(params);
		CouponCarveupConfigInfo entity=couponCarveupConfigInfoService.getById(params.getId());
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(YNEnum.YES.getCode().equals(entity.getState())) {
			throw new BusinessException(CouponErrorEnum.Coupon_UpperLower);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		
		entity.setName(params.getName());
		entity.setIsUseLimit(params.getIsUseLimit());
		entity.setUseFullFund(params.getUseFullFund());
		entity.setTotalNum(params.getTotalNum());
		entity.setStartDate(LocalDateTimeUtils.parse_yyyyMMdd(params.getStartDate()));
		entity.setEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getEndDate()));
		entity.setIndateDay(params.getIndateDay());
		entity.setIndateHour(params.getIndateHour());
		entity.setIndateMinute(params.getIndateMinute());
		entity.setCarveupUserNum(params.getCarveupUserNum());
		entity.setCarveupFund(params.getCarveupFund());
		entity.setCarveupWay(params.getCarveupWay());
		entity.setUsableObjType(params.getUsableObjType());
		entity.setUsableObjNo(StrSplitUtils.list2string(params.getUsableObjNo()));
		entity.setUseDateType(params.getUseDateType());
		entity.setUseStartDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseStartDate()));
		entity.setUseEndDate(LocalDateTimeUtils.parse_yyyyMMddHHmmss(params.getUseEndDate()));
		entity.setUseEndDay(params.getUseEndDay());
		entity.setSimulateFriendSetting(params.getSimulateFriendSetting());
		entity.setMemberGetNumType(params.getMemberGetNumType());
		entity.setMemberGetNum(params.getMemberGetNum());
		entity.setIsRemind(params.getIsRemind());
		entity.setRemindDay(params.getRemindDay());
		entity.setOtherSetting(StrSplitUtils.list2string(params.getOtherSetting()));
		entity.addEditParam(userInfo);
		couponCarveupConfigInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
	@GetMapping("/get")
	@ApiOperation(value="获取", response=CouponCarveUpConfigModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		CouponCarveupConfigInfo entity=couponCarveupConfigInfoService.getById(id);
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		CouponCarveUpConfigModel data=CouponChangeUtils.getCouponCarveUpConfig(entity);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/remove")
	@ApiOperation(value="删除", response=BaseResult.class)
	public BaseResult remove(@RequestParam("id")Integer id) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		CouponCarveupConfigInfo entity=couponCarveupConfigInfoService.getById(id);
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(YNEnum.YES.getCode().equals(entity.getState())) {
			throw new BusinessException(CouponErrorEnum.Coupon_UpperLower);
		}
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		couponCarveupConfigInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
	
	
	
}
